/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package air.servlet;

import air.sb.AirRemote;
import air.sb.SASRemote;
import air.sb.LuftRemote;
import java.io.*;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 *
 * @author mrxyz
 * Handles the call from HttpRequestHandler and handels the BookTrip request by calling
 * the session AirBean and redirects to the right JSPX-page
 */
public class BookTrip extends HttpServlet {
    @EJB
    private AirRemote airBean;
    @EJB
    private SASRemote sASBean;
    @EJB
    private LuftRemote luftBean;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        int totalNrTrips = 0;
        try {

            int nrOfFlights = 1;
            ArrayList<String> name = new ArrayList<String>();
            ArrayList<String> flights = new ArrayList<String>();
            ArrayList<String> sRow = new ArrayList<String>();
            ArrayList<String> pNr = new ArrayList<String>();
            ArrayList<Integer> sNr = new ArrayList<Integer>();
            ArrayList<String> arrDate = new ArrayList<String>();
            ArrayList<String> depDate = new ArrayList<String>();
            ArrayList<String> airline = new ArrayList<String>();

            String[] names = request.getParameterValues("name");
            String[] pNrs = request.getParameterValues("pNr");
            String[] routes = request.getParameterValues("flightNr");
            String[] seatNr = request.getParameterValues("sNr");
            String[] seatRow = request.getParameterValues("sRow");
            String[] arrDates = request.getParameterValues("arrDate");
            String[] depDates = request.getParameterValues("depDate");
            String[] airlines = request.getParameterValues("airline");

            totalNrTrips = airBean.getNrTrips() + sASBean.getNrTrips() + luftBean.getNrTrips();
            //Counting the number of flights
            String temp1 = routes[0];
            for(int i = 1; i < names.length; i++){
                if(temp1.equals(routes[i])){
                    break;
                }
                nrOfFlights++;
            }

            for (int k = 0; k < names.length; k++) {
                for (int i = 0; i < nrOfFlights; i++) {
                    //Integer.parseInt(request.getParameter("nrOfFlights")
                    name.add(names[k]);
                    pNr.add(pNrs[k]);
                    flights.add(routes[i]);
                    airline.add(airlines[i]);
                    System.out.println("SeatNr " + seatNr.length + "  " + i );
                    sNr.add(Integer.parseInt(seatNr[i]));
                    arrDate.add(arrDates[i]);
                    depDate.add(depDates[i]);
                    sRow.add(seatRow[i]);

                }
            }
            //LuftHansa
            if(airline.get(0).equals("Deutsche Lufthansa")){
                luftBean.bookTrip(sNr, sRow, name, pNr, arrDate, depDate, flights, airline, nrOfFlights, totalNrTrips);
            }
            //SAS
            else if(airline.get(0).equals("Scandinavian Airlines SAS")){
                sASBean.bookTrip(sNr, sRow, name, pNr, arrDate, depDate, flights, airline, nrOfFlights, totalNrTrips);
            }
            //AA
            else{
                airBean.bookTrip(sNr, sRow, name, pNr, arrDate, depDate, flights, airline, nrOfFlights, totalNrTrips);
            }

            //change the other flights db
            for(int i = 1; i < airline.size(); i++){
                //SAS
                if(!airline.get(i).equals(airline.get(0)) && airline.get(i).equals("SAS")){
                    sASBean.bookOther(sNr.get(i), sRow.get(i), name.get(i), pNr.get(i), flights.get(i));
                }
                //LuftHansa
                else if(!airline.get(i).equals(airline.get(0)) && airline.get(i).equals("LuftHansa")){
                    luftBean.bookOther(sNr.get(i), sRow.get(i), name.get(i), pNr.get(i), flights.get(i));
                }
                //AA
                else if(!airline.get(i).equals(airline.get(0)) && airline.get(i).equals("AA")){
                    airBean.bookOther(sNr.get(i), sRow.get(i), name.get(i), pNr.get(i), flights.get(i));
                }
            }
        } finally {
            response.sendRedirect("Receipt.jspx");
            out.close();
        }
    }



    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
